package com.goldsign.pay.provider.userbase.service.impl;

import com.goldsign.pay.provider.userbase.service.UserBaseService;
import com.goldsign.pay.provider.userbase.to.CreateUserReqDTO;
import com.goldsign.pay.provider.userbase.vo.LoginIdInfoDO;
import com.goldsign.pay.provider.userbase.vo.UserInfoDO;
import com.goldsign.pay.common.exception.GcServiceException;
import com.goldsign.pay.common.string.StringUtil;
import com.goldsign.pay.common.util.Constant;
import com.goldsign.pay.provider.userbase.dao.UserServiceDao;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.SQLException;

/**
 * Created by xiaojingjing on 2018/5/9.
 */
@Service
public class UserBaseServiceImpl implements UserBaseService {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private UserServiceDao userDao;

    @Override
    public void createUser(CreateUserReqDTO req, String userNo) throws GcServiceException {
        try {
            //生成用户信息
            insertUserInfo(req, userNo);

            //将手机号加入到登录id表，用户后续登录使用
            LoginIdInfoDO loginIdInfoDO = new LoginIdInfoDO();
            loginIdInfoDO.setUser_no(userNo);
            if(StringUtil.isNotEmpty(req.getMob_no())){
                loginIdInfoDO.setLogin_id(userNo);
                userDao.insertLoginIdInfo(loginIdInfoDO);
            }

            //将手机号加入到登录id表，用户后续登录使用
            if(StringUtil.isNotEmpty(req.getMob_no())){
                loginIdInfoDO.setLogin_id(req.getMob_no());
                userDao.insertLoginIdInfo(loginIdInfoDO);
            }


        } catch (SQLException e) {
            throw new GcServiceException("USERBASE_DATABASE_ERROR", Constant.USERBASE_DATABASE_ERROR);
        } catch (Exception e) {
            throw new GcServiceException("USERBASE_SYSTERM_ERROR", Constant.USERBASE_SYSTERM_ERROR);
        }
    }

    private void insertUserInfo(CreateUserReqDTO req, String userNo) throws SQLException,Exception {

        UserInfoDO userInfo = new UserInfoDO();
        userInfo.setUser_no(userNo);
        userInfo.setUser_status("0");
        userInfo.setUser_type(StringUtil.isEmpty(req.getUser_type(), "1"));
        userInfo.setUser_first_name(req.getUser_first_name());
        userInfo.setUser_last_name(req.getUser_last_name());
        userInfo.setMob_no_country_code(req.getMob_no_country_code());
        userInfo.setMob_no(req.getMob_no());
        userInfo.setEmail(req.getEmail());
        userInfo.setTel_no(req.getTel_no());
        userInfo.setLogin_passwd(req.getLogin_passwd());
        userInfo.setDeal_passwd(req.getDeal_passwd());
        userInfo.setRealname_status(StringUtil.isEmpty(req.getRealname_status(),"0"));

        userDao.insertUserInfo(userInfo);
    }

    @Override
    public UserInfoDO getUserInfoByMobNo(String mobNo) throws GcServiceException {
        UserInfoDO userInfo = null;

        try {
            userInfo = userDao.getUserInfoByMobNo(mobNo);
            if (userInfo == null) {
                throw new GcServiceException("MOB_NO_IS_NOT_EXISTS", Constant.MOB_NO_IS_NOT_EXISTS);
            }
        } catch (GcServiceException e) {
            throw e;
        } catch (SQLException e) {
            logger.error(mobNo + "_" + e.getMessage());
            throw new GcServiceException("USERBASE_DATABASE_ERROR", Constant.USERBASE_DATABASE_ERROR);
        }
        return userInfo;
    }

    @Override
    public UserInfoDO getUserInfoByLoginId(String loginId) throws GcServiceException {
        try {
            UserInfoDO userInfo = userDao.getUserInfoByLoginId(loginId);

            return userInfo;
        } catch (SQLException e) {
            throw new GcServiceException("USERBASE_DATABASE_ERROR", Constant.USERBASE_DATABASE_ERROR);
        }
    }

}
